package com.yao.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yao.domain.User;
import com.yao.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {

    //    --------------------------------页面跳转------------------------------

    //请求映射
    //http://localhost:8080/quick => http://localhost:8080/user/quick
    //value就不用说了，method是请求方式，当前只有GET请求才有效,params表示在请求该资源时，必须携带有参数username
    @RequestMapping(value = "/quick",method = RequestMethod.GET,params = {"username"})
    public String save(){
        System.out.println("Controller save running...");
        //加个"/"，表示从根路径下找资源，也就是http://localhost:8080/
        return "success";
    }

    @RequestMapping(value = "/quick2")
    public ModelAndView save2(){
        /*
            Model => 模型  封装数据
            View  => 视图  展示数据
         */
        //设置模型数据和视图名称
        ModelAndView modelAndView = new ModelAndView("success");
        modelAndView.addObject("username","yao");
        return modelAndView;
    }

    @RequestMapping(value = "/quick3")
    //由Spring提供的modelAndView对象
    public ModelAndView save3(ModelAndView modelAndView){
        modelAndView.addObject("username","yao2");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/quick4")
    //由Spring提供的model对象，设置好数据后，返回一个字符串，相当于返回视图的名称
    public String save4(Model model){
        model.addAttribute("username","姚大帅哥");
        return "success";
    }

    @RequestMapping(value = "/quick5")
    //原始Servlet提供的request对象
    public String save5(HttpServletRequest request){
        request.setAttribute("username","abc");
        return "success";
    }

    //    --------------------------------回写数据------------------------------

    @RequestMapping(value = "/quick6")
    //原始Servlet提供的response对象
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello SpringMVC");
    }

    @ResponseBody //★告诉SpringMVC不要进行页面跳转，而是回写数据
    @RequestMapping(value = "/quick7")
    public String save7(){
        return "hello SpringMVC!!";
    }

    @ResponseBody //★告诉SpringMVC不要进行页面跳转，而是回写数据
    @RequestMapping(value = "/quick8")
    public String save8(){
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

    @ResponseBody
    @RequestMapping(value = "/quick9")
    public String save9() throws JsonProcessingException {
        User user = new User();
        user.setUsername("lisi");
        user.setAge(30);
        //使用json的转换工具将对象转换为json格式的字符串,再返回
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonStr = objectMapper.writeValueAsString(user);
//        return "{\"username\":\"zhangsan\",\"age\":18}";
        return jsonStr;
    }

    @ResponseBody
    @RequestMapping(value = "/quick10")
    public User save10(){
        User user = new User();
        user.setUsername("lisi");
        user.setAge(30);
        return user;
    }

    //获取基本类型参数
    @ResponseBody
    @RequestMapping(value = "/quick11")
    public void save11(String username,int age){
        System.out.println(username);
        System.out.println(age);
    }

    //获取POJO类型参数
    @ResponseBody
    @RequestMapping(value = "/quick12")
    public void save12(User user){
        System.out.println(user);
    }

    //获取数组类型参数
    @ResponseBody
    @RequestMapping(value = "/quick13")
    public void save13(String[] strs){
        System.out.println(Arrays.asList(strs));
    }

    //获取集合类型参数
    @ResponseBody
    @RequestMapping(value = "/quick14")
    public void save14(VO vo){
        System.out.println(vo);
    }

    //获取集合类型参数2
    @ResponseBody
    @RequestMapping(value = "/quick15")
    public void save15(@RequestBody List<User> userList){
        System.out.println(userList);
    }

    //参数绑定注解@requestParam
    //**value**：与请求参数名称
    //**required**：此在指定的请求参数是否必须包括，默认是true，提交时如果没有此参数则报错
    //**defaultValue**：当没有指定请求参数时，则使用指定的默认值赋值
    @ResponseBody
    @RequestMapping(value = "/quick16")
    public void save16(@RequestParam("name") String username){
        System.out.println(username);
    }

    //localhost:8080/user/quick17/zhangsan
    //获得Restful风格的参数
    @ResponseBody
    @RequestMapping(value = "/quick17/{username}")
    public void save17(@PathVariable("username") String username){
        System.out.println(username);
    }

    //自定义类型转换器
    @ResponseBody
    @RequestMapping(value = "/quick18")
    public void save18(Date date){
        System.out.println(date);
    }

    //获得Servlet相关API
    @ResponseBody
    @RequestMapping(value = "/quick19")
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }

    //获得请求头
    @ResponseBody
    @RequestMapping(value = "/quick20")
    public void save20(@RequestHeader(value = "User-Agent", required = false) String user_agent){
        System.out.println(user_agent);
    }

    //获得请求头
    @ResponseBody
    @RequestMapping(value = "/quick21")
    public void save21(@CookieValue(value = "JSESSIONID", required = false) String jSessionId){
        System.out.println(jSessionId);
    }

    //获得请求头
    @ResponseBody
    @RequestMapping(value = "/quick22")
    public void save22(String username, MultipartFile uploadFile, MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        //上传文件的名称
        String filename = uploadFile.getOriginalFilename();
        System.out.println(filename);
        //保存文件
        uploadFile.transferTo(new File("E:\\download\\" + filename));

        String filename2 = uploadFile2.getOriginalFilename();
        System.out.println(filename2);
        uploadFile.transferTo(new File("E:\\download\\" + filename2));
    }

    //获得请求头
    @ResponseBody
    @RequestMapping(value = "/quick23")
    public void save23(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        for (MultipartFile multipartFile : uploadFile) {
            String filename = multipartFile.getOriginalFilename();
            System.out.println(filename);
            multipartFile.transferTo(new File("E:\\download\\" + filename));
        }
    }

}
